<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.title"
          placeholder="请输入标题"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.helpType"
          placeholder="请选择分类"
          style="width: 100%"
        >
          <el-option
            v-for="item in answerType"
            :key="item.dicValue"
            :label="item.dicLabel"
            :value="item.dicValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.status"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加问题</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="helpTypeTxt" label="分类"></el-table-column>
        <el-table-column prop="readNum" label="浏览量（次）"></el-table-column>
        <el-table-column prop="addName" label="发布人"></el-table-column>
        <el-table-column
          prop="addTime"
          label="发布时间"
          width="160px"
        ></el-table-column>
        <el-table-column label="发布状态">
          <template slot-scope="scope">
            <span v-if="scope.row.pubStatus == 0">保存</span>
            <span v-if="scope.row.pubStatus == 1">发布</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.status == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 1"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item
                  command="publish"
                  v-if="scope.row.pubStatus == 0 && $checkPermission(101)"
                  >发布</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 1 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer
      :title="title"
      size="888px"
      :wrapperClosable="false"
      :visible.sync="visible"
      :destroy-on-close="true"
    >
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="标题" required>
          <el-input
            v-model.trim="form.title"
            clearable
            maxlength="30"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类" required>
          <el-select
            clearable
            v-model="form.helpType"
            placeholder="请选择分类"
            style="width: 100%"
          >
            <el-option
              v-for="item in answerType"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" required>
          <wang-editor :content="form.content" @getContent="getContent" />
        </el-form-item>
        <el-form-item label="附件">
          <web-upload @upload="upload" type="file" />
          <el-table :data="file" v-if="file.length > 0">
            <el-table-column prop="name" label="名称">
              <template slot-scope="scope">
                <el-tooltip
                  popper-class="ellipsis-tip"
                  :content="scope.row.name"
                  placement="top-start"
                >
                  <span class="ellipsis">{{ scope.row.name }}</span>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column prop="size" label="大小"></el-table-column>
            <el-table-column
              prop="content"
              label="操作"
              width="100px"
              align="center"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="downloadFile(scope.row)"
                  >下载</el-link
                >
                <el-link
                  type="danger"
                  :underline="false"
                  style="margin-left: 10px"
                  @click="remove(scope.row)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-primary" @click="save(0)">保存</span>
          <span class="wrap-btn wrap-success" @click="save(1)">发布</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog
      title="查看"
      :visible.sync="isLook"
      :destroy-on-close="true"
      width="888px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="标题" :span="2">
          {{ lockData.title }}
        </el-descriptions-item>
        <el-descriptions-item label="分类">
          {{ lockData.helpTypeTxt }}
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <span v-if="lockData.status == 0">启用</span>
          <span v-if="lockData.status == 1">停用</span>
        </el-descriptions-item>
        <el-descriptions-item label="内容" :span="2">
          <div class="wrap-html" v-html="lockData.content"></div>
        </el-descriptions-item>
        <el-descriptions-item label="附件" :span="2">
          <el-table :data="lockData.file" v-if="lockData.file.length > 0">
            <el-table-column prop="name" label="名称">
              <template slot-scope="scope">
                <el-tooltip
                  popper-class="ellipsis-tip"
                  :content="scope.row.name"
                  placement="top-start"
                >
                  <span class="ellipsis">{{ scope.row.name }}</span>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column prop="size" label="大小"></el-table-column>
            <el-table-column
              prop="content"
              label="操作"
              width="50px"
              align="center"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="downloadFile(scope.row)"
                  >下载</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-descriptions-item>
        <el-descriptions-item label="发布人">
          {{ lockData.addName }}
        </el-descriptions-item>
        <el-descriptions-item label="发布时间">
          {{ lockData.addTime }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      answerType: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加问题',
      form: {},
      file: [],
      isLook: false,
      lockData: { file: [] },
    }
  },
  created() {
    this.listDic()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/admin/company/help/page', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    dateChange(val) {
      // 时间范围发生变化
      if (val) {
        this.formSearch.param.start = val[0]
        this.formSearch.param.stop = val[1]
      } else {
        this.formSearch.param.start = ''
        this.formSearch.param.stop = ''
      }
    },
    add() {
      //添加
      this.title = '添加问题'
      this.form = {}
      this.file = []
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios
            .get('/admin/company/help/find?id=' + row.id)
            .then((data) => {
              this.lockData = { file: [] }
              if (data.code == 200) {
                this.lockData = data.data
                this.lockData.file = JSON.parse(data.data.audio)
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'edit':
          //编辑
          this.title = '编辑问题'
          this.$axios
            .get(`/admin/company/help/find?id=${row.id}`)
            .then((data) => {
              this.form = {}
              this.file = []
              if (data.code == 200) {
                this.form = data.data
                this.file = JSON.parse(data.data.audio)
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get(
                  `/admin/company/help/updatePubStatus?id=${row.id}&status=100`
                )
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
        case 'publish':
          //发布
          this.$axios
            .get(`/admin/company/help/updatePubStatus?id=${row.id}&status=1`)
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'changeStatus':
          //启用停用
          const status = row.status == 0 ? 1 : 0
          this.$axios
            .get(
              `/admin/company/help/updateStatus?id=${row.id}&status=${status}`
            )
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
      }
    },
    save(type) {
      //保存
      if (!this.form.title) {
        this.$message.error('请输入标题')
        return
      }
      if (!this.form.helpType) {
        this.$message.error('请选择分类')
        return
      }
      if (!this.form.content) {
        this.$message.error('请输入内容')
        return
      }
      this.form.pubStatus = type
      this.form.audio = JSON.stringify(this.file)
      this.$axios.post('/admin/company/help/add', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getContent(html) {
      this.form.content = html
    },
    upload(data) {
      //上传
      this.file.push(data)
    },
    downloadFile(item) {
      //下载附件
      const a = document.createElement('a')
      a.download = item.name
      a.href = item.fileDomain + item.url
      a.click()
    },
    remove(item) {
      //删除
      var index = this.form.file.indexOf(item)
      if (index !== -1) {
        this.form.file.splice(index, 1)
      }
    },
    listDic() {
      // 问题分类
      this.$axios.get('/admin/dic/listDic?type=answerType').then((data) => {
        this.answerType = []
        if (data.code == 200) {
          this.answerType = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
  },
}
</script>
